package com.ruoyi.web.controller.jiaxiao;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.JxStaff;
import com.ruoyi.system.service.IJxStaffService;

@RestController
@RequestMapping("/jiaxiao/staff")
public class JxStaffController extends BaseController {

    @Autowired
    private IJxStaffService jxStaffService;

    @PreAuthorize("@ss.hasPermi('jiaxiao:staff:list')")
    @GetMapping("/list")
    public TableDataInfo list(JxStaff query) {
        startPage();
        List<JxStaff> list = jxStaffService.selectJxStaffList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('jiaxiao:staff:query')")
    @GetMapping("/{staffId}")
    public AjaxResult getInfo(@PathVariable Long staffId) {
        return success(jxStaffService.selectJxStaffById(staffId));
    }

    @PreAuthorize("@ss.hasPermi('jiaxiao:staff:add')")
    @Log(title = "职工", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JxStaff entity) {
        return toAjax(jxStaffService.insertJxStaff(entity));
    }

    @PreAuthorize("@ss.hasPermi('jiaxiao:staff:edit')")
    @Log(title = "职工", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JxStaff entity) {
        return toAjax(jxStaffService.updateJxStaff(entity));
    }

    @PreAuthorize("@ss.hasPermi('jiaxiao:staff:remove')")
    @Log(title = "职工", businessType = BusinessType.DELETE)
    @DeleteMapping("/{staffIds}")
    public AjaxResult remove(@PathVariable Long[] staffIds) {
        return toAjax(jxStaffService.deleteJxStaffByIds(staffIds));
    }

    @PreAuthorize("@ss.hasPermi('jiaxiao:staff:export')")
    @Log(title = "职工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(JxStaff query) {
        List<JxStaff> list = jxStaffService.selectJxStaffList(query);
        ExcelUtil<JxStaff> util = new ExcelUtil<>(JxStaff.class);
        return util.exportExcel(list, "职工数据");
    }

    @PreAuthorize("@ss.hasPermi('jiaxiao:staff:import')")
    @Log(title = "职工", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<JxStaff> util = new ExcelUtil<>(JxStaff.class);
        List<JxStaff> list = util.importExcel(file.getInputStream());
        int success = 0;
        for (JxStaff s : list) {
            if (updateSupport && s.getStaffId() != null) {
                jxStaffService.updateJxStaff(s);
            } else {
                jxStaffService.insertJxStaff(s);
            }
            success++;
        }
        return AjaxResult.success("导入成功" + success + "条");
    }

    @PostMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<JxStaff> util = new ExcelUtil<>(JxStaff.class);
        return util.importTemplateExcel("职工数据");
    }
}


